﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using RCTools;

namespace exercice4
    {
    class Program
        {
        static int Main(string[] args)
            {
            Dictionary<String, double> map = giveParameters(args);

            double a = map["-a"];
            double p;
            bool isVerbose;
            if (map.ContainsKey("-p")) { p = map["-p"]; }
            else { p = MyMath.DEFAULT_SQRT_PRECISION; }

            if (map.ContainsKey("-v")) { isVerbose = Convert.ToBoolean(map["-v"]); }
            else { isVerbose = false; }
            Console.Out.Write(MyMath.sqrt(a, p, isVerbose));
            return 0;
            }

        private static Dictionary<string, double> giveParameters(string[] args)
            {
            List<String> listParameters = new List<String>();
            listParameters.Add("-a");
            listParameters.Add("-p");
            listParameters.Add("-v");

            try
                {
                Dictionary<String, double> map = new Dictionary<string, double>();

                if (args.Count() == 1)
                    {
                    map.Add("-a", Convert.ToDouble(args[0]));
                    }
                else
                    {
                    int i;
                    for (i = 0; i < args.Length - 1; i += 2)
                        {
                        if (args[i] == "-v") { map.Add(args[i], 1); --i; }
                        else { map.Add(args[i], Convert.ToDouble(args[i + 1])); }
                        }

                    map.Add("-a", Convert.ToDouble(args[i]));
                    }

                if (!map.ContainsKey("-a")) { throw new Exception("Number to handle not found !"); }
                if (!RCTools.Container.isAllKeysInList(map, listParameters)) { throw new Exception("Error of parameter !"); }
                if (map["-a"] < 0) { throw new Exception("Sqrt of a negative number is not possible in real set !"); }

                return map;
                }
            catch (Exception e)
                {
                System.Console.WriteLine(e.Message);
                System.Console.WriteLine("\nHow to use : ");
                System.Console.WriteLine("\nsqrt.exe [-p | ] [-v | ] number");
                System.Console.WriteLine("-p : precision used when comparing the square of the number calculated with the initial number (optional) - default precision is 1e-9");
                System.Console.WriteLine("-v : verbose (optional) - display all the steps of calculation and the time ");
                System.Console.WriteLine("\nExample : \nsqrt.exe -p 0.0001 -v 54");
                System.Console.WriteLine("sqrt.exe -v -p 0.000001 4578.235");
                System.Console.WriteLine("sqrt.exe -p 0.01 175.56");
                System.Console.WriteLine("sqrt.exe 2456.5");
                System.Environment.Exit(-1);
                return null;
                }
            }
        }
    }
